package com.cskaoyan.service.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.role.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Autowired
    MarketModuleLevelAMapper marketModuleLevelAMapper;

    @Autowired
    MarketModuleLevelBMapper marketModuleLevelBMapper;

    @Autowired
    MarketModuleAuthMapper marketModuleAuthMapper;

    @Autowired
    MarketAdminMapper marketAdminMapper;

    /**
     * @author zjq
     * 查询管理员角色信息
     * @date 2022/11/19 17:43
     */
    @Override
    public CommonData query(Integer id, String name,Integer page,Integer limit) {


        if(page!=null&&limit!=null){
            PageHelper.startPage(page, limit);
        }

        MarketRoleExample example = new MarketRoleExample();
        //
//        example.setDistinct(true);

        MarketRoleExample.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if(name!=null&&!"".equals(name)) {
            criteria.andNameLike(name);
        }
//        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        List<RoleVo> marketRoles = marketRoleMapper.selectAllRole();
        PageInfo pageInfo = new PageInfo(marketRoles);


        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public CommonData queryList(Integer id, String name, Integer page, Integer limit) {


        PageHelper.startPage(page, limit);
        MarketRoleExample example = new MarketRoleExample();



        MarketRoleExample.Criteria criteria = example.createCriteria();

        if (id != null && !"".equals(id)) {//StringUtils isEmpty
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {

            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketRoles);
        CommonData commonData = new CommonData();
        commonData.setLimit(limit);
        commonData.setPage(pageInfo.getPageNum());
        commonData.setTotal((int) pageInfo.getTotal());
        commonData.setPages(pageInfo.getPages());
        commonData.setList(marketRoles);
        return commonData;

    }

    @Override
    public MarketRole create(MarketRole role) {
        //判断是否重名
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        if(role.getName()!=null&&!"".equals(role.getName())){
            criteria.andNameEqualTo(role.getName());
        }
        criteria.andDeletedEqualTo(false);

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        if(!marketRoles.isEmpty()){
            return null;
        }
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);
        marketRoleMapper.insertSelective(role);
        int id = marketRoleMapper.selectIdByName(role.getName());
        role.setId(id);
        return role;

    }

    @Override
    public int update(MarketRole role) {
        //判断是否重名
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        if(role.getName()!=null&&!"".equals(role.getName())){
            criteria.andNameEqualTo(role.getName());
        }
        criteria.andDeletedEqualTo(false);

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        if(!marketRoles.isEmpty()){
            return 0;
        }
        int i = marketRoleMapper.updateByPrimaryKeySelective(role);
        return i;
    }

    @Override
    @Transactional
    public int delete(MarketRole role) {
        int i = marketRoleMapper.deleteByPrimaryKey(role.getId());
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(role.getId());
        marketPermissionMapper.deleteByExample(example);
        return i;
    }

    @Override
    public MarketModuleLevelBVo.PermissionsVo queryModulePermissions(Integer id) {
        List<MarketModuleLevelAVo> systemPermissions=new ArrayList<>();
        List<String> assignedPermissions = new ArrayList<>();
        assignedPermissions=marketPermissionMapper.selectPermissionsById(id);
//        systemPermissions=marketPermissionMapper.selectAllModulePermission();
        MarketModuleLevelAExample example = new MarketModuleLevelAExample();

        List<MarketModuleLevelA> marketModuleLevelAS = marketModuleLevelAMapper.selectByExample(example);
        for (MarketModuleLevelA marketModuleLevelA : marketModuleLevelAS) {
            MarketModuleLevelAVo marketModuleLevelAVo = new MarketModuleLevelAVo();
            marketModuleLevelAVo.setId(marketModuleLevelA.getName());
            marketModuleLevelAVo.setLabel(marketModuleLevelA.getLabel());
            List<MarketModuleLevelBVo> children = new ArrayList<>();
            for (Integer child : marketModuleLevelA.getChildren()) {
                MarketModuleLevelB marketModuleLevelB = marketModuleLevelBMapper.selectByPrimaryKey(child);
                MarketModuleLevelBVo marketModuleLevelBVo = new MarketModuleLevelBVo();
                marketModuleLevelBVo.setId(marketModuleLevelB.getName());
                marketModuleLevelBVo.setLabel(marketModuleLevelB.getLabel());
                List<AuthVo> moduleAuths = new ArrayList<>();
                for (Integer authId  : marketModuleLevelB.getChildren()) {
                    MarketModuleAuth marketModuleAuth = marketModuleAuthMapper.selectByPrimaryKey(authId);
                    AuthVo authVo = new AuthVo();
                    authVo.setApi(marketModuleAuth.getApi());
                    authVo.setLabel(marketModuleAuth.getLabel());
                    authVo.setId(marketModuleAuth.getName());
                    moduleAuths.add(authVo);
                }
                marketModuleLevelBVo.setChildren(moduleAuths);
                children.add(marketModuleLevelBVo);
            }
            marketModuleLevelAVo.setChildren(children);
            systemPermissions.add(marketModuleLevelAVo);
        }

        MarketModuleLevelBVo.PermissionsVo permissionsVo = new MarketModuleLevelBVo.PermissionsVo();
        permissionsVo.setSystemPermissions(systemPermissions);
        permissionsVo.setAssignedPermissions(assignedPermissions);
        return permissionsVo;
    }

    /**
     * 要更新3张表
     * admin+admin2auth+
     * @param id
     * @param permissions
     */
    @Override
    @Transactional
    public void updatePermissions(Integer id,List<String> permissions) {
//        Subject subject = SecurityUtils.getSubject();
//        MarketAdmin marketAdmin = (MarketAdmin)subject.getPrincipal();
//        String username = marketAdmin.getUsername();
//        int id = marketAdminMapper.selectIdByName(username);
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(id);
        marketPermissionMapper.deleteByExample(example);
        for (String permission : permissions) {
            MarketPermission marketPermission = new MarketPermission();
            marketPermission.setRoleId(id);
            marketPermission.setPermission(permission);
            marketPermission.setUpdateTime(new Date());
            marketPermission.setAddTime(new Date());
            marketPermissionMapper.insertSelective(marketPermission);
        }
    }
}
